<?php

class SportsEventManager
{
	private $_db;
	
	public function __construct(PDO $db)
	{
		$this->setDb($db);
	}
	
	public function setDb($db)
	{
		$this->_db = $db;
	}
	
	public function add(SportsEvent $sportEvent)
	{
		//Fonction qui va ajouter un sportsEvent dans la table sportsevents
		$reqInsert = $this->_db->prepare("INSERT INTO sportsevents SET fullName= :fullName, shortName= :shortName, sport_id= :sport_id, description= :description");
		$reqInsert->bindValue(":fullName", $sportEvent->fullName());
		$reqInsert->bindValue(":shortName", $sportEvent->shortName());
		$reqInsert->bindValue(":sport_id", $sportEvent->sport_id());
		$reqInsert->bindValue(":description", $sportEvent->description());
		$reqInsert->execute();
		$sportEvent->hydrate(array(
			'id' => $this->_db->lastInsertId()
		));
	}
	public function delete(SportsEvent $sportEvent)
	{
		//Fonction qui va supprimer un sportsEvent dans la table sportsevents
		$reqDelete = $this->_db->prepare("DELETE FROM sportsevents WHERE id= :id");
		$reqDelete->bindValue(":id", $sportEvent->id());
		$reqDelete->execute();
	}
	public function update(SportsEvent $sportEvent)
	{
		//Fonction qui va mettre à jour un sportsEvent dans la table sportsevents
		$reqUpdate = $this->_db->prepare("UPDATE sportsevents SET fullName= :fullName, shortName= :shortName, sport_id= :sport_id, description= :description WHERE id= :id");
		$reqUpdate->bindValue(":id", $sportEvent->id(), PDO::PARAM_INT);
		$reqUpdate->bindValue(":fullName", $sportEvent->fullName(), PDO::PARAM_STR);
		$reqUpdate->bindValue(":shortName", $sportEvent->shortName(), PDO::PARAM_STR);
		$reqUpdate->bindValue(":sport_id", $sportEvent->sport(), PDO::PARAM_INT);
		$reqUpdate->bindValue(":description", $sportEvent->description(), PDO::PARAM_INT);
		$reqUpdate->execute();
	}
	public function get($search)
	{
		//Fonction qui va chercher un sportsEvent dans la table sportsevents à partir du critère de recherche search
		if(is_int($search))
		{
			$reqSelect = $this->_db->prepare("SELECT * FROM sportsevents WHERE id= :id");
			$reqSelect->bindValue(":id", $search);
			$reqSelect->execute();
			$sportsEvent = $reqSelect->fetch(PDO::FETCH_ASSOC);
			return new SportsEvent($sportsEvent);
		}
		if(is_string($search))
		{
			$reqSelect = $this->_db->prepare("SELECT * FROM sportsevents WHERE fullName LIKE %:name%");
			$reqSelect->bindValue(":name", $search);
			$reqSelect->execute();
			$sportsEvent = $reqSelect->fetch(PDO::FETCH_ASSOC);
			return new SportsEvent($sportsEvent);
		}
	}
	public function listEventsBySports(Sport $sport)
	{
		//Fonction qui va restituer la liste des sportsEvents relatifs au sport dont l'id est $sport
		$reqSelect = $this->_db->prepare("SELECT * FROM sportsevents WHERE sport_id= :sport");
		$reqSelect->bindValue(":sport", $sport->id());
		$reqSelect->execute();
		$sportsEvents = array();
		while($sportsEvent = $reqSelect->fetch(PDO::FETCH_ASSOC))
		{
			$sportsEventL = new SportsEvent($sportsEvent);
			$sportsEvents[] = $sportsEventL;
		}
		return $sportsEvents;
	}

}